<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            background: red;
            position:absolute;
            left: 0px;
        }

    </style>
</head>
<body>
    <div class="box"></div>
</body>
<script>
function move(ele,dir,target,cb){
    let start = parseInt( getComputedStyle(ele,null)[dir]);
    let speed = 2*(target-start)>=0?1:-1;
    setTimeout(() => {
        start += speed;
        ele.style[dir] = start + "px";
        if(Math.abs(start-target)<5){
            // console.log("运动完成");
            cb && cb();
        }else{
            move(ele,dir,target,cb);  
        }
    }, 10);
}
let ele =  document.querySelector(".box");
// move(ele,"left",300);
// move(ele,"top",300);
// 回调控制执行顺序；
// 回调地狱：函数作为参数层层嵌套-->解决？？自定义事件
move(ele,"left",300,()=>{
    console.log("向右运动完成");
    move(ele,"top",300,()=>{
        console.log("向下运动完成");
        move(ele,"left",0,()=>{
            console.log("向左运动完成");
            move(ele,"top",0,()=>{
                console.log("运动完成");
            })
        })
    })
})



</script>
</html>